import type { Rule } from 'ant-design-vue/es/form';
export function validatenull(val: any) {
  if (typeof val === 'boolean') {
    return false;
  }
  if (typeof val === 'number') {
    return false;
  }
  if (val instanceof Array) {
    if (val.length == 0) return true;
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === '{}') return true;
  } else {
    if (
      val == 'null' ||
      val == null ||
      val == 'undefined' ||
      val == undefined ||
      val == '' ||
      val == 0
    )
      return true;
    return false;
  }
  return false;
}

export function validatePhone(rule: any, value: string, callback: any) {
  const reg: any = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
  if (value === '' || value === undefined || value == null) {
    callback()
  } else {
    if ((!reg.test(value)) && value !== '') {
      callback(new Error('请输入正确的电话号码'))
    } else {
      callback()
    }
  }
}
export async function validatePassword(rule: Rule, value: string) {
  const validatePasswordReg = /^(?=.*[~!@#$%^&*])(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[~!@#$%^&*A-Za-z\d]{8,20}$/g;
  const flag = validatePasswordReg.test(value);
  if (flag) {
    return Promise.resolve();
  } else {
    return Promise.reject(new Error('密码必须为8~20位数字+字母（含大小写）+特殊字符'));
  }
}

/**
 * @description 输入框的校验【校验是用户名称还是手机号】 用于接口居多
 */
export const isaccountType = (account: string) => {
    const reg: RegExp = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
    return reg.test(account) ? 'mobile' : 'userName';
};